<?php
/**
 * Created by JetBrains PhpStorm.
 * User: zhenzhen
 * Date: 12-9-17
 * Time: 下午3:35
 * To change this template use File | Settings | File Templates.
 */
class useroperationControl extends Control{
    function register_show(){
        $db=M('profession');
        $this->assign('pro',$db->all());
     $this->display("zc_user_register.html");
    }
    function register(){//处理注册
      if(empty($_POST['check'])){
          $this->error('你没有同意');
      }
      $db=M('user');
        $_POST['password']=md5($_POST['password']);
        $db->add();
        $uid=$db->get_insert_id();
        $db->table('userinfo');
        $_POST['uid']=$uid;
        $_POST['regtime']=Time();
        $db->add();
    }
    function registered(){//注册成功后
     $this->display();
    }
    function checkuser(){//ajax验证用户名
        $username = $_POST['param'];
        $db=M('user');

        $num=count($db->where("username='".$username."'")->find()['uid']);
        if($num>0){
            echo '用户 '.$username.' 已存在';
        }else{
            echo "y";
        }
    }
    function checkemail(){//ajax验证邮箱
        $email = $_POST['param'];
        $db=M('userinfo');
        $num=count($db->where("email='".$email."'")->find()['email']);
        if($num>0){
            echo '邮箱已存在';
        }else{
            echo "y";
        }
    }
    function login_show(){//用户登录}
  /*      if(!empty($_SESSION['login'])){
            $this->success('您已经登陆','index');
        }*/
    $this->display('zc_user_login.html');
    }
    function login(){//处理登陆
    if(empty($_GET['u'])){
        $this->success('请直接登陆');
    }
    $db=M('user');
    
    $user=$_POST['username'];
    $pass=$_POST['password'];
    $jizhu=empty($_POST['check'])?null:$_POST['check'];
    $res=$db->where("username='".$user."'")->find();
    if(!empty($res) && md5($pass)==$res['password']){
      if($jizhu==1){
        setcookie('username',$res['username'],time()+3600*3,'/');
        setcookie('uid',$res['uid'],time()+3600*3,'/');
        $this->session_set('login',1);
   
        if(!empty($_GET['f'])){
            $fget='/'.$_GET['f'].'/'.$_GET['f1'];//组合后的url
            $this->go(__APP__.$fget);
        }else{
            $this->success('已经成功登陆','index');
                }
        
        }else{
          $this->session_set('username',$res['username']);
          $this->session_set('login',1);
          $this->session_set('uid', $res['uid']);
          
         if(!empty($_GET['f'])){
            $fget='/'.$_GET['f'].'/'.$_GET['f1'];//组合后的url
            $this->go(__APP__.$fget);
        }else{
          $this->success('已经成功登陆',__WEB__.'/index/izcool');
        }
        
        }
    }else{
        $this->error('用户名或密码错误!');
    }
    }
    function ajaxlogin(){//ajax登陆
        $db=M('user');
        $user=$_POST['t_loginname'];
        $pass=$_POST['t_password'];
        $jizhu=empty($_POST['t_auto'])?null:$_POST['t_auto'];
        $res=$db->where("username='$user'")->find();
        if(!empty($res) && md5($pass)==$res['password']){
            if($jizhu==1){
                setcookie('username',$res['username'],time()+3600*3,'/');
                setcookie('uid',$res['uid'],time()+3600*3,'/');
                $this->session_set('login',1);
                echo $res['username'];
            }else{
                $this->session_set('username',$res['username']);
                $this->session_set('login',1);
                $this->session_set('uid',$res['uid']);
                echo $res['username'];
            }
        }else{
            echo 0;
        }
    }
    function logout(){//退出
        setcookie("username",NULL,time()-3600,'/');
        $this->session_del('username');
        $this->session_del('uid');
        $this->session_set('login',0);
        //$this->session_set('uid','no');
        if(empty($_COOKIE['username']) && empty($_SESSION['username'])){
            $this->success('已经退出',__WEB__.'/index/index');
        }

    }

    function getpassword_show(){//找回密码
    $this->display("zc_user_getpassword.html");
    }
    function getpassword(){//处理找回密码
       if(empty($_POST['email']) && empty($_POST['email1'])){
          $this->go('getpassword_show');
       }
       $email=$_POST['email'];
       $db=v('user');
       $db->view=array(
            'userinfo'=>array(
                'join_type'=>'inner',
                'on'=>'userinfo.uid=user.uid',
                             )
        );
        $userRes=$db->field('email,username,password')->where("email='$email'")->all();
        if(!$userRes){
            $this->error('没有此邮件,请重新填写');
        }

        $user=$userRes[0]['username'];
        $pass=$userRes[0]['password'];
        $time=time();
        $emtit='站酷网-请尽快更改密码(48小时有效)';
        $href=__CONTROL__."/get_password_s/user/$user/md5/$pass/time/$time";
        $body="<p style='height:30px;background:#FFBA00;text-align:center;'>站酷网[邮件在48小时候失效]</p><p>请点击链接更改密码.</p><br/><a href='".$href."'>$href</a>";
        //echo $body;
        $send=zcoolmail('duanliyang1@126.com',$userRes[0]['email'],'站酷网,请尽快更改密码',$body);
        if($send){
            $this->success('邮件已经发送,请尽快更改密码');
        }else{
            $this->error('发送失败,请稍后重试 -_-');
        }
        //$email_obj->send($email,'','duanliyang1@live--请尽快填写更改密码的信息!',);

    }
    function get_password_s(){//处理找回密码
        $user=$_GET['user'];
        $pass=$_GET['md5'];
        $time=$_GET['time'];
        $timenow=time();
        if(($timenow-$time)/3600>=2){//如果时间大于两天就失效
            $this->success('邮件已经失效,请重新发送',__CONTROL__.'/getpassword_show');
        };
        $db=v('user');
        $db->view=array(
            'userinfo'=>array(
                'join_type'=>'inner',
                'on'=>'userinfo.uid=user.uid',
            )
        );
        $userres=$db->where("username='$user'")->all();
        $p_user=$userres[0]['username'];
        $p_pass=$userres[0]['password'];
        if($pass!=$p_pass){
            echo ('身份出错');
        }else{
            $this->assign('uid',$userres[0]['uid']);
            $this->display('zc_get_password_show.html');
        }

    }
    function get_password_s1(){
        if(empty($_GET['u'])){
            $this->success('出错!');
        }
        $db=M('user');
        $user=$db->where("uid=".$_POST['uid'])->find();
        $user=$user['username'];
        $_POST['password']=md5($_POST['password']);
        if($db->save()){
            $this->assign('username',$user);
            $this->display("zc_get_password.html");
        };
    }
    
    
   
}